﻿@model Citi.Scheduler.WebUi.Models.RoutineExecutionModel
@{
    Layout = null;
}
<html>
<head>
    <title>Error</title>
</head>
<body>
    <p>
        Dear(s),
    </p>
    <p>
        You're receiving this message because you were assigned to @Model.Routine.Name routine.
        So when some error occurs you will be notified.
    </p>
    <p>
        Below follows the error information:
    </p>
    <table>
        <tr>
            <th>
                Date
            </th>
            <th>
                Status
            </th>
            <th>
                Details
            </th>
        </tr>
        @foreach (var history in Model.RoutineExecutionHistories)
        {
            <tr>
                <td>@history.Date.ToString("dd/MM/yyyy hh:mm")
                </td>
                <td>@history.Status.GetDescription()
                </td>
                <td>@history.Details
                </td>
            </tr>
        }
    </table>
    <p>
        Error history</p>
    <table>
        <tr>
            <th>
                Ocurred on
            </th>
            <th>
                Status
            </th>
            <th>
                Description
            </th>
        </tr>
        @foreach (var error in Model.RoutineErrors)
        {
            <tr>
                <td>@error.OcurredOn.ToString("dd/MM/yyyy hh:mm")
                </td>
                <td>@error.Status.GetDescription()
                </td>
                <td>@error.ErrorDescription
                </td>
            </tr>
        }
    </table>
    <p>
        Email escalation history</p>
    @{
        var emails = Model.RoutineErrors.SelectMany(x => x.RoutineErrorEmails).ToList();

        if (emails.Any())
        {
        <table>
            <tr>
                <th>
                    Sent at
                </th>
                <th>
                    Users
                </th>
            </tr>
            @foreach (var email in emails)
            {
                <tr>
                    <td>@email.SentDate.ToString("dd/MM/yyyy hh:mm")
                    </td>
                    <td>@email.Escalation.AllUsers
                    </td>
                </tr>
            }
        </table>
        }
        else
        {
        <span>No emails were sent.</span>
        }

        <p>
            Next escalations (if no action is taken)</p>
        
        var nextEscalations = Model.RoutineErrors.SelectMany(x => x.NextEscalations).ToList();

        if (nextEscalations.Any())
        {
       
        <table>
            <tr>
                <th>
                    Will be sent at
                </th>
                <th>
                    Users
                </th>
            </tr>
            @foreach (var nextEscalation in nextEscalations)
            {
                <tr>
                    <td>@Model.RoutineErrors.First().OcurredOn.AddMinutes(nextEscalation.Minutes).ToString("dd/MM/yyyy hh:mm")
                    </td>
                    <td>@nextEscalation.AllUsers
                    </td>
                </tr>
            }
        </table>
        }
        else
        {
        <span>No escalation was configured.</span>
        }
    }
</body>
</html>
